<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--      &lt;!&ndash;部门数据&ndash;&gt;
            <el-col :span="4" :xs="24">
              <div class="head-container">
                <el-input
                  v-model="deptName"
                  placeholder="请输入部门名称"
                  clearable
                  size="small"
                  prefix-icon="el-icon-search"
                  style="margin-bottom: 20px"
                />
              </div>
              <div class="head-container">
                <el-tree
                  :data="deptOptions"
                  :props="defaultProps"
                  :expand-on-click-node="false"
                  :filter-node-method="filterNode"
                  ref="tree"
                  default-expand-all
                  @node-click="handleNodeClick"
                />
              </div>
            </el-col>-->
      <table>
        <tr>
          <td><span>资源标题 :</span></td>
          <td><el-input v-model="form.title" placeholder="请输入资源标题" style="width: 240px"></el-input></td>

        </tr>
        <br>
        <tr>
          <td>资源图片:</td>
          <td><el-upload class="avatar-uploader"
                         :action="''"
                         :auto-upload="false"
                         :show-file-list="false"
                         :on-change="handleAccessoryChangeIcon">
            <img v-if="form.img" :src="form.img" class="avatar" style="height: 150px;">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload></td>
        </tr>
        <br>
        <tr>
          <td>资源地址:</td>
          <td><el-input v-model="form.link" placeholder="请输入资源地址" style="width: 240px"></el-input></td>
        </tr>
        <br>
        <tr>
          <td><span>资源分类:</span></td>
          <td  style="position: absolute;z-index: 10001;text-align: center">

            <el-select
              v-model="form.classify"
              placeholder="资源分类"
              clearable
              size="small"
              style="width: 240px"
            >
              <el-option
                v-for="dict in type"
                :key="dict.dict_value"
                :label="dict.dict_label"
                :value="dict.dict_label"
              />
            </el-select>
          </td>
        </tr>
        <br>

        <br>
        <tr>
          <td><span>资源简介:</span></td>
          <td>
            <!-- v-model="form.describe"-->
            <el-input
              style="width: 420px"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请输入内容"
              v-model="form.introduction">
            </el-input>
          </td>

        </tr>
        <br>
        <tr>
          <td></td>
          <td>
            <el-button type="primary" @click="submit">提交</el-button>
            <el-button type="info" @click="reset" style="margin-left: 30px;">重置</el-button></td>
        </tr>
      </table>

    </el-row>
  </div>
</template>
<script>
  import { listFile , getFile , addFile , updateFile ,delFile ,uploadFileImg} from "@/api/monitor/file";
  import { listActivity ,addActivity ,updateActivity ,getActivity} from "@/api/monitor/activity";
  import { listResource , delResource ,getResource,updateResource} from "@/api/monitor/resource";
  import { getToken } from "@/utils/auth";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import Editor from '@/components/wangEditor'

  export default {
    components: { Treeselect  ,Editor },
    data() {
      return {
        isClear: false,
        detail:"",
        user: {},
        sexs: [],
        option:[],
        id_card_type: [],
        status:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 默认密码
        initPassword: undefined,
        // 日期范围
        dateRange: [],
        // 状态数据字典
        statusOptions: [],
        // 性别状态字典
        Options: [],
        // 岗位选项
        postOptions: [],
        // 角色选项
        roleOptions: [],
        // 表单参数
        form: {},
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,

        },
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 1000,
          name: undefined,
          status: undefined
        },
        /*        // 表单校验
                rules: {
                  userName: [
                    { required: true, message: "用户名称不能为空", trigger: "blur" }
                  ],
                  nickName: [
                    { required: true, message: "用户昵称不能为空", trigger: "blur" }
                  ],

                  email: [
                    {
                      type: "email",
                      message: "'请输入正确的邮箱地址",
                      trigger: ["blur", "change"]
                    }
                  ],
                  phonenumber: [
                    {
                      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                      message: "请输入正确的手机号码",
                      trigger: "blur"
                    }
                  ]
                }*/
        type:[]
      };
    },
    created() {
      const id = this.$route.params && this.$route.params.id;
      this.getResource(id);
      this.getDicts("sys_study_resource").then(response => {
        this.type = response.data.items;
      });
    },

    methods: {
      submit () {
        updateResource(this.form).then(rs =>{
          this.msgSuccess("修改成功");
        })
      },
      // 表单重置
      reset() {
        this.form = {
          subject_type: undefined,
          type: undefined,
          title: undefined,
          activity_time: undefined,
          position: undefined,
          content: undefined,
          activity_require: undefined,
          max_sign_up_time: undefined,
          sign_up_people_count: undefined,
          remarks: undefined,
        };
        this.resetForm("form");
      },
      //获取频道
      getListChannle(){
        listChannel(this.addDateRange(this.queryParams, this.dateRange)).then(rs =>{
          this.option = rs.data.items;
          console.log(this.option)
        })
      },
      getResource(activityId){
        getResource(activityId).then(rs =>{
          this.form = rs.data;
        })
      },
      /*文档附件上传*/
      handleAccessoryChangeIcon(file, fileList) {//选中文件触发的change事件
        console.log(file)
        const isJPG = file.raw.type === 'image/jpeg'
        const isPNG = file.raw.type === 'image/png'
        // const isLt2M = file.raw.size / 1024 / 1024 < 0.5  //限制上传文件的大小
        if (!isPNG && !isJPG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!')
          return false
        } else {
          this.form.img = URL.createObjectURL(file.raw);//赋值图片的url，用于图片回显功能
          this.uploadImg(file)//调用上传文件api接口
          this.$forceUpdate()
        }
      },
      uploadImg(file){//文件上传操作
        let formData=new FormData()
        formData.append('file',file.raw)
        uploadFileImg(formData).then(res=>{
          console.log(res)
          if (res.code == '20000') {
            this.msgSuccess(res.data.msg);
            this.form.img = res.data[0].url;
            console.log(this.form.img)
          }
        })
      },

    }

  };
</script>
<style>
  .myQuillEditor{
    height: 300px;
  }
</style>
